import SyRequest from "./request";
import { BASE_URL, TIME_OUT } from "./config";
import localStorage from "@/utils/local-storage";
import useMainStore from "@/stores/main/main";
import { ElLoading } from "element-plus";
import type { RawAxiosRequestHeaders } from "axios";

export default new SyRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestSuccessFn: (config) => {
      const token = localStorage.get("token");
      if (token) {
        (
          config.headers as RawAxiosRequestHeaders
        ).Authorization = `Bearer ${token}`;
        // ;(config.headers as AxiosHeaders).set(
        //   'Authorization',
        //   `Bearer ${token}`
        // )
      }

      useMainStore().showLoading = true;
      return config;
    },
    responseSuccessFn: (res) => {
      useMainStore().showLoading = false;

      return res;
    },
  },
});
